using System;
using System.Collections;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.Common;
using TXLooker.Common.Debuging;
using TXLooker.WorkItemExtention.Core;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.UI
{
    public partial class WorkItemForm : Form
    {
        protected IOlWorkItemProcessor OlProcessor
        {
            get { return this.m_olProcessor; }
            set
            {
                if( object.ReferenceEquals( value, null ) )
                    throw new ArgumentException();

                this.m_olProcessor = value;
            }
        }
        private IOlWorkItemProcessor m_olProcessor = null;

        protected WorkItem WorkItem
        {
            get { return this.m_workItemCtrl.Item;  }
            private set 
            {
                if( object.ReferenceEquals(value, null) )
                    throw new NullReferenceException();

                this.m_workItemCtrl.Item = value; 
            }
        }

        public delegate void ApplyChangesEventHandler();

        public event ApplyChangesEventHandler ChangesApplied;

        protected object OlObject
        {
            get { return this.m_olObject; }
            private set
            {
                if( object.ReferenceEquals( value, null ) )
                    throw new NullReferenceException();

                this.m_olObject = value;
            }
        }
        private object m_olObject = null;

        private WorkItemForm()
        {
            InitializeComponent();
            m_ToolStrip.Visible = false;
        }
        public WorkItemForm( WorkItem wiItem ) : this()
        {
            this.WorkItem = wiItem;
        }
        public WorkItemForm( WorkItem wiItem, IOlWorkItemProcessor olProcessor ) : this(wiItem)
        {
            this.OlProcessor = olProcessor;
            m_ToolStrip.Visible = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <param name="olItem">Outlook object that coresponds to work item</param>
        /// <param name="olProcessor"></param>
        /// 
        public WorkItemForm( WorkItem wiItem, object olObject, IOlWorkItemProcessor olProcessor ) : this(wiItem, olProcessor)
        {
            this.OlObject = olObject;
        }
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad(e);
            this.Text = string.Format("Work Item Settings : {0}", this.m_workItemCtrl.Item.Type.Name);

            this.m_tbtnCreateEmail.Text = Resources.UI_EMAIL_CAPTION;
            this.m_tbtnCreateMeeting.Text = Resources.UI_MEETING_CAPTION;

            IntPtr hWnd = this.Handle;
            DllImportCommon.SetForegroundWindow(hWnd);
        }

        private void OnOK( object sender, EventArgs e )
        {
            bool bClose = true;

            try
            {
                WorkItem item = this.m_workItemCtrl.Item;
                ArrayList list = item.Validate();

                bool bSuccess = ( list == null || (list != null && list.Count == 0) );

                if( !bSuccess )
                {
                    string message = string.Empty;

                    foreach( Field field in list )
                        message += string.Format( "Field '{0}' is not valid", field.Name ) + Environment.NewLine;

                    MessageBox.Show( this, message, Resources.EXTENTION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Warning );

                    bClose = false;
                    return;
                }

                Cursor oldCursor = this.Cursor;

                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    //save new data into TFS
                    item.Save();
                    ChangesApplied();
                    //ubdate OlObject by new work item data
                    //if( this.OlProcessor != null )
                    //    this.OlProcessor.OlWorkItemUpdate( item, OlObject );
                }
                catch
                { }
                finally
                {
                    this.Cursor = oldCursor;
                }
            }
            catch( System.Exception ce )
            {
                DebugManager.ShowErrorDescription( string.Format( "{0}{1}{2}", "Work Item can not be saved.", System.Environment.NewLine, ce.ToString() ) );
            }
            finally
            {
                if( bClose) this.Close();
            }
        }

        private void OnCancel( object sender, EventArgs e )
        {
            this.Close();
        }

        private void OnCreateEmail( object sender, EventArgs e )
        {
            this.OlProcessor.CreateOlWorkItemEmail( this.WorkItem );
        }

        private void OnCreateMeeting( object sender, EventArgs e )
        {
            this.OlProcessor.CreateOlWorkItemMeeting( this.WorkItem );
        }

    }
}